<?php

namespace common\models\db;

use Yii;
use wodrow\yii2wtools\tools\ArrayHelper;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%custom}}".
 *
 * @author
 * @property-read array $statusDesc
 * @property User $bindUser
 * @property CustomBankCard[] $bankCards
 */
class Custom extends \common\models\db\tables\Custom
{
    const SCENARIO_TEST = 'test';
    const STATUS_ACTIVE = 10;
    const ID_TYPE_A = 'A';
    const ID_TYPE_E = 'E';
    const ID_TYPE_F = 'F';
    const GENDER_M = 'M';
    const GENDER_F = 'F';
    const OCCUPATION_0001 = '0001';
    const OCCUPATION_0002 = '0002';
    const OCCUPATION_0003 = '0003';
    const OCCUPATION_0004 = '0004';
    const OCCUPATION_0005 = '0005';
    const OCCUPATION_0006 = '0006';
    const OCCUPATION_0007 = '0007';
    const OCCUPATION_0008 = '0008';
    const OCCUPATION_0009 = '0009';

    public function getStatusDesc()
    {
        return [
            self::STATUS_ACTIVE => "正常",
        ];
    }

    public function getIdTypeDesc()
    {
        return [
            self::ID_TYPE_A => "身份证",
            self::ID_TYPE_E => "港澳居民往来大陆通行证",
            self::ID_TYPE_F => "台湾居民往来大陆通行证",
        ];
    }

    public function getGenderDesc()
    {
        return [
            self::GENDER_M => "男",
            self::GENDER_F => "女",
        ];
    }

    public function getOccupationDesc()
    {
        return [
            self::OCCUPATION_0001 => '国家机关、党群组织、企业、事业单位负责人',
            self::OCCUPATION_0002 => '专业技术人员',
            self::OCCUPATION_0003 => '办事人员和有关人员',
            self::OCCUPATION_0004 => '商业工作人员',
            self::OCCUPATION_0005 => '服务性工作人员',
            self::OCCUPATION_0006 => '农、林、牧、渔、水利业生产人员',
            self::OCCUPATION_0007 => '生产、运输设备操作人员及有关人员',
            self::OCCUPATION_0008 => '军人',
            self::OCCUPATION_0009 => '不便分类的其他从业人员',
        ];
    }

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = ArrayHelper::merge($behaviors, [
            'timestamp' => [
                'class' => TimestampBehavior::class,
            ],
            'blameable' => [
                'class' => BlameableBehavior::class,
                'createdByAttribute' => false,
                'updatedByAttribute' => false,
            ],
        ]);
        return $behaviors;
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios = ArrayHelper::merge($scenarios, [
            self::SCENARIO_TEST => [],
        ]);
        return $scenarios;
    }

    public function rules()
    {
        $rules = parent::rules();
        foreach ($rules as $k => $v) {
            if ($v[1] == 'required'){
                $rules[$k][0] = array_diff($rules[$k][0], ['created_at', 'updated_at', 'created_by', 'updated_by']);
            }
        }
        $rules = ArrayHelper::merge($rules, [
            ['email', 'email'],
        ]);
        return $rules;
    }

    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels = ArrayHelper::merge($attributeLabels, [
            'id_type' => "证件类型",
            'gender' => "性别",
            'birth_day' => "出生日期",
            'occupation' => "职业",
            'issue_office' => "发证机关",
        ]);
        return $attributeLabels;
    }

    public function beforeValidate()
    {
        if (parent::beforeValidate()){
            // TODO: Change the autogenerated stub
            return true;
        }else{
            return false;
        }
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)){
            // TODO: Change the autogenerated stub
            return true;
        }else{
            return false;
        }
    }

    public function beforeDelete()
    {
        if (parent::beforeDelete()){
            // TODO: Change the autogenerated stub
            return true;
        }else{
            return false;
        }
    }

    public function afterFind()
    {
        parent::afterFind();
        // TODO: Change the autogenerated stub
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        // TODO: Change the autogenerated stub
        $this->_deleteCaches();
    }

    public function afterValidate()
    {
        parent::afterValidate();
        // TODO: Change the autogenerated stub
    }

    public function afterRefresh()
    {
        parent::afterRefresh();
        // TODO: Change the autogenerated stub
        $this->_deleteCaches();
    }

    public function afterDelete()
    {
        parent::afterDelete();
        // TODO: Change the autogenerated stub
        $this->_deleteCaches();
    }

    protected function _deleteCaches()
    {}

    public function test()
    {
        $test = self::instance();
        $test->setScenario(self::SCENARIO_TEST);
        $test->save();
        var_dump($test->toArray());
    }

    /**
     * @return \yii\db\ActiveQuery|User
     */
    public function getBindUser()
    {
        return $this->hasOne(User::class, ['id' => "bind_user_id"]);
    }

    /**
     * @return \yii\db\ActiveQuery|CustomBankCard[]|null
     */
    public function getBankCards()
    {
        return $this->hasMany(CustomBankCard::class, ['custom_id' => "id"]);
    }
}
